<?php 
// Stringbuffer for Code
$output = "";

// Actionhandler
if(isset($_POST['login'])){
	if($_POST['login_username'] != "" && $_POST['login_password'] != ""){
		if(isUserValid($_POST['login_username'], $_POST['login_password'])==0){
			$output .= "<br><font color='red'>Username or password is wrong!</font>";
			$_SESSION['loggedin'] = false;
		}else{
			$output .= "<br><font color='green'>You have been logged in!</font>";
			$_SESSION['loggedin'] = true;
		}
	}else{
		$output .= "<br><font color='red'>Please fill in all the fields!</font>";
	}
}else if(isset($_POST['register'])){
	$user_information = array();
	$user_information[0] = $_POST['username'];
	$user_information[1] = $_POST['password'];
	$user_information[2] = $_POST['name'];
	$user_information[3] = $_POST['surname'];
	$user_information[4] = $_POST['street'];
	$user_information[5] = $_POST['zip'];
	$user_information[6] = $_POST['country'];
	$user_information[7] = $_POST['re_password'];
	
	$return_value = registerUser($user_information);

	if($return_value == 2){
		$output .= "<br><font color='red'>Please fill out all the fields!</font>";
		$_SESSION['loggedin'] = false;
	}else if($return_value == 3){
		$output .= "<br><font color='red'>Password don't match!</font>";
		$_SESSION['loggedin'] = false;
	}else if($return_value == 4){
		$output .= "<br><font color='red'>Username exists! Please change!</font>";
		$_SESSION['loggedin'] = false;
	}else{
		$output .= "<br><font color='green'>You have been registered!</font>";
		$_SESSION['loggedin'] = true;
	}
}

// HTML-Code
if(!isset($_SESSION['loggedin']) || $_SESSION['loggedin'] == false){
	$output .= '
		<h1>Login</h1>
		
		<form action="#" method="post">
			<table>
				<tr>
					<td>Username</td>
					<td><input type=text name=login_username /></td>
				</tr>
				<tr>
					<td>Password</td>
					<td><input type=password name=login_password /></td>
				</tr>
				<tr>
					<td><input type=submit name=login value=Login /></td>
				</tr>
			</table>
			
			<h1>Register</h1>
			<table>
				<tr>
					<td>Username</td>
					<td><input type=text name=username /></td>
				</tr>
				<tr>
					<td>Password</td>
					<td><input type=password name=password /></td>
				</tr>
				<tr>
					<td>Repeat Password</td>
					<td><input type=password name=re_password /></td>
				</tr>
				<tr>
					<td>Name</td>
					<td><input type=text name=name /></td>
				</tr>
				<tr>
					<td>Surname</td>
					<td><input type=text name=surname /></td>
				</tr>
				<tr>
					<td>Street</td>
					<td><input type=text name=street /></td>
				</tr>
				<tr>
					<td>ZIP</td>
					<td><input type=text name=zip /></td>
				</tr>
				<tr>
					<td>Country</td>
					<td><input type=text name=country /></td>
				</tr>
				<tr>
					<td><input type=submit name=register value=Register /></td>
				</tr>
			</table>
		</form>';
}

//Output
echo $output;
?>